package stack;

import javax.xml.bind.annotation.XmlType;
import java.util.Arrays;

public class MyStack {
    private int[] elem;
    public int usedsize = 0;

    private static final int DEFUALT_CAPACITY = 10;
    public MyStack(){
        elem = new int[DEFUALT_CAPACITY];
    }

    private boolean isFull(){
            return this.usedsize == this.elem.length;
    }
    public void push(int val){
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,this.elem.length * 2);
        }
        elem[usedsize] = val;
        usedsize++;
    }
    private boolean isEmpty(){
        return this.usedsize == 0;
    }
    public void pop(){
        if(isEmpty()){
            System.out.println("栈为空!");
        }
        this.usedsize--;
    }
    public int peek(){
        if(isEmpty()){
            System.out.println("栈为空!");
        }
        int oldValue = this.elem[this.usedsize - 1];
        return oldValue;
    }

    public void display(){
        for(int i = 0; i < this.usedsize ; i++){
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }
    @Override
    public String toString() {
        return Arrays.toString(elem) + " ";
    }
}
